import nimx/keyboard
import tables

const virtualKeyMapping: Table[int, VirtualKey] = {
  0x1D: VirtualKey.Zero,
  0x12: VirtualKey.One,
  0x13: VirtualKey.Two,
  0x14: VirtualKey.Three,
  0x15: VirtualKey.Four,
  0x17: VirtualKey.Five,
  0x16: VirtualKey.Six,
  0x1A: VirtualKey.Seven,
  0x1C: VirtualKey.Eight,
  0x19: VirtualKey.Nine,

  0x0C: VirtualKey.Q,
  0x0D: VirtualKey.W,
  0x0E: VirtualKey.E,
  0x0F: VirtualKey.R,
  0x11: VirtualKey.T,
  0x10: VirtualKey.Y,
  0x20: VirtualKey.U,
  0x22: VirtualKey.I,
  0x1F: VirtualKey.O,
  0x23: VirtualKey.P,
  0x00: VirtualKey.A,
  0x01: VirtualKey.S,
  0x02: VirtualKey.D,
  0x03: VirtualKey.F,
  0x05: VirtualKey.G,
  0x04: VirtualKey.H,
  0x26: VirtualKey.J,
  0x28: VirtualKey.K,
  0x25: VirtualKey.L,
  0x06: VirtualKey.Z,
  0x07: VirtualKey.X,
  0x08: VirtualKey.C,
  0x09: VirtualKey.V,
  0x0B: VirtualKey.B,
  0x2D: VirtualKey.N,
  0x2E: VirtualKey.M,

  0x7A: VirtualKey.F1,
  0x78: VirtualKey.F2,
  0x63: VirtualKey.F3,
  0x76: VirtualKey.F4,
  0x60: VirtualKey.F5,
  0x61: VirtualKey.F6,
  0x62: VirtualKey.F7,
  0x64: VirtualKey.F8,
  0x65: VirtualKey.F9,
  0x6D: VirtualKey.F10,
  0x67: VirtualKey.F11,
  0x6F: VirtualKey.F12,
  0x69: VirtualKey.F13,
  0x6B: VirtualKey.F14,
  0x71: VirtualKey.F15,
  0x6A: VirtualKey.F16,
  0x40: VirtualKey.F17,
  0x4F: VirtualKey.F18,
  0x50: VirtualKey.F19,
  0x5A: VirtualKey.F20,

  0x47: VirtualKey.NumLock,
  0x43: VirtualKey.KeypadMultiply,
  0x35: VirtualKey.Escape,
  0x30: VirtualKey.Tab,
  0x24: VirtualKey.Return,
  0x3B: VirtualKey.LeftControl,
  0x38: VirtualKey.LeftShift,
  0x3C: VirtualKey.RightShift,
  0x3E: VirtualKey.RightControl,
  0x73: VirtualKey.Home,
  0x7E: VirtualKey.Up,
  0x7B: VirtualKey.Left,
  0x7C: VirtualKey.Right,
  0x77: VirtualKey.End,
  0x7D: VirtualKey.Down,
  # VK_INSERT: VirtualKey.Insert,
  0x75: VirtualKey.Delete,
  # VK_PAUSE: VirtualKey.Pause,
  0x31: VirtualKey.Space,

  0x52: VirtualKey.Keypad0,
  0x53: VirtualKey.Keypad1,
  0x54: VirtualKey.Keypad2,
  0x55: VirtualKey.Keypad3,
  0x56: VirtualKey.Keypad4,
  0x57: VirtualKey.Keypad5,
  0x58: VirtualKey.Keypad6,
  0x59: VirtualKey.Keypad7,
  0x5B: VirtualKey.Keypad8,
  0x5C: VirtualKey.Keypad9,

  0x45: VirtualKey.KeypadPlus,
  0x33: VirtualKey.Backspace,
  # VK_CANCEL: VirtualKey.Cancel,
  0x41: VirtualKey.KeypadPeriod,
  0x4E: VirtualKey.KeypadMinus,
  0x2C: VirtualKey.Slash,
  0x74: VirtualKey.PageUp,
  0x79: VirtualKey.PageDown,

  0x1B: VirtualKey.Minus,
  0x18: VirtualKey.Equals,
  0x21: VirtualKey.LeftBracket,
  0x1E: VirtualKey.RightBracket,
  0x29: VirtualKey.Semicolon,
  0x27: VirtualKey.Apostrophe,
  0x32: VirtualKey.Backtick,
  0x2A: VirtualKey.BackSlash,
  0x2B: VirtualKey.Comma,
  0x2F: VirtualKey.Period,
  0x3A: VirtualKey.LeftAlt,
  0x39: VirtualKey.CapsLock,
  # # SDL_SCANCODE_SCROLLLOCK: VirtualKey.ScrollLock,



  # # SDL_SCANCODE_NONUSBACKSLASH: VirtualKey.NonUSBackSlash,

  # # SDL_SCANCODE_KP_ENTER: VirtualKey.KeypadEnter,
  0x4B: VirtualKey.KeypadDivide,
  # # SDL_SCANCODE_PRINTSCREEN: VirtualKey.PrintScreen,
  0x3D: VirtualKey.RightAlt,
  # # SDL_SCANCODE_MUTE: VirtualKey.Mute,
  0x49: VirtualKey.VolumeDown,
  0x48: VirtualKey.VolumeUp,
  0x7F: VirtualKey.Power,
  # # SDL_SCANCODE_KP_EQUALS: VirtualKey.KeypadEquals,
  # # SDL_SCANCODE_KP_PLUSMINUS: VirtualKey.KeypadPlusMinus,



  # # SDL_SCANCODE_KP_COMMA: VirtualKey.KeypadComma,

  0x37: VirtualKey.LeftGUI,
  0x36: VirtualKey.RightGUI,
  # # SDL_SCANCODE_STOP: VirtualKey.Stop,
  # # SDL_SCANCODE_AGAIN: VirtualKey.Again,
  # # SDL_SCANCODE_UNDO: VirtualKey.Undo,
  # # SDL_SCANCODE_COPY: VirtualKey.Copy,
  # # SDL_SCANCODE_PASTE: VirtualKey.Paste,
  # # SDL_SCANCODE_FIND: VirtualKey.Find,
  # # SDL_SCANCODE_CUT: VirtualKey.Cut,
  # # SDL_SCANCODE_HELP: VirtualKey.Help,
  # # SDL_SCANCODE_MENU: VirtualKey.Menu,
  # # SDL_SCANCODE_CALCULATOR: VirtualKey.Calculator,
  # # SDL_SCANCODE_SLEEP: VirtualKey.Sleep,
  # # SDL_SCANCODE_MAIL: VirtualKey.Mail,
  # # SDL_SCANCODE_AC_BOOKMARKS: VirtualKey.AcBookmarks,
  # # SDL_SCANCODE_COMPUTER: VirtualKey.Computer,
  # # SDL_SCANCODE_AC_BACK: VirtualKey.AcBack,
  # # SDL_SCANCODE_AC_FORWARD: VirtualKey.AcForward,
  # # SDL_SCANCODE_EJECT: VirtualKey.Eject,
  # # SDL_SCANCODE_AUDIONEXT: VirtualKey.AudioNext,
  # # SDL_SCANCODE_AUDIOPLAY: VirtualKey.AudioPlay,
  # # SDL_SCANCODE_AUDIOPREV: VirtualKey.AudioPrev,
  # # SDL_SCANCODE_AC_HOME: VirtualKey.AcHome,
  # # SDL_SCANCODE_AC_REFRESH: VirtualKey.AcRefresh,
  # # SDL_SCANCODE_KP_LEFTPAREN: VirtualKey.KeypadLeftPar,
  # # SDL_SCANCODE_KP_RIGHTPAREN: VirtualKey.KeypadRightPar,
  # # SDL_SCANCODE_AC_SEARCH: VirtualKey.AcSearch,
  # # SDL_SCANCODE_ALTERASE: VirtualKey.AltErase,
  # # SDL_SCANCODE_CANCEL: VirtualKey.Cancel,
  # # SDL_SCANCODE_BRIGHTNESSDOWN: VirtualKey.BrightnessDown,
  # # SDL_SCANCODE_BRIGHTNESSUP: VirtualKey.BrightnessUp,
  # # SDL_SCANCODE_DISPLAYSWITCH: VirtualKey.DisplaySwitch,
  # # SDL_SCANCODE_KBDILLUMTOGGLE: VirtualKey.IlluminateToggle,
  # # SDL_SCANCODE_KBDILLUMDOWN: VirtualKey.IlluminateDown,
  # # SDL_SCANCODE_KBDILLUMUP: VirtualKey.IlluminateUp
}.toTable()

template virtualKeyFromNative*(kc: int): VirtualKey = virtualKeyMapping.getOrDefault(kc)
